package com.lai.gulimall.cart.controller;

import com.lai.common.constant.CartConstant;
import com.lai.common.to.UserInfoTo;
import com.lai.common.utils.R;
import com.lai.gulimall.cart.Interceptor.CartInterceptor;
import com.lai.gulimall.cart.service.CartService;
import com.lai.gulimall.cart.vo.CartItemVo;
import com.lai.gulimall.cart.vo.CartVo;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {


    @Autowired
    CartService cartService;


    @GetMapping("/cart.html")
    public String cartListPage(Model model, HttpServletRequest request) throws ExecutionException, InterruptedException {
        /*UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        System.out.println(userInfoTo);*/
        String key = "";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if(name.equals(CartConstant.TEMP_USER_COOKIE_NAME)){
                key = cookie.getValue();
            }
        }
        CartVo cartVo = cartService.getCart(key);
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * @param skuId 商品 skuId
     * @param num   数量
     * @return
     */
    @GetMapping("/addProductToCart")
    public String addProductToCart(@RequestParam Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItemVo cartItemVo = cartService.addProductToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addProductCartSuccess.html";
    }


    /**
     * 防止用户重复刷新页面一直把商品添加购物车
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addProductCartSuccess.html")
    public String addProductCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        //再次查询购物车数据，重定向到成功页面
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItemVo);
        return "success";
    }


    @GetMapping("/checkItem")
    @ResponseBody
    public R checkItem(@RequestParam("skuId")Long skuId,@RequestParam("check") Integer check){
        CartVo cartVo = cartService.checkItem(skuId, check);
        return R.ok().put("data",cartVo);
    }

    @GetMapping("/countItem")
    @ResponseBody
    public R countItem(@RequestParam("skuId")Long skuId,@RequestParam("num") Integer num){
        CartVo cartVo = cartService.countItem(skuId, num);
        return R.ok().put("data",cartVo);
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/clearCart")
    public String clearCart(){
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        String cartKey = "";
        if (userInfoTo.getUserId() != null) {
            cartKey = CartConstant.CART_PREFIX + userInfoTo.getUserId();
        } else {
            cartKey = CartConstant.CART_PREFIX + userInfoTo.getUserKey();
        }
        cartService.clearCart(cartKey);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/getUserCartItems/{memberId}")
    @ResponseBody
    public R getUserCartItemsById(@PathVariable("memberId") Long memberId){
        List<CartItemVo> cartItemVoList = cartService.getUserCartItemsById(memberId);
        return R.ok().put("data",cartItemVoList);
    }



}
